package com.itheima.service.impl;

import com.itheima.common.constant.MessageConstant;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.pojo.dto.OrderDTO;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Autowired
    private MemberMapper memberMapper;

    private static final String CAPTCHA_KEY_PREFIX = "captcha:";
    private static final String DEFAULT_PASSWORD = "123456";

    @Override
    public Order findById(Integer id) {
        Order order = orderMapper.findById(id);
        return order;
    }

    @Transactional
    @Override
    public void submitOrder(OrderDTO orderDTO) {
        // 判断验证码
        String key = CAPTCHA_KEY_PREFIX + orderDTO.getTelephone();
        String code = (String) redisTemplate.opsForValue().get(key);
        if (code == null || orderDTO.getTelephone() == null) {
            throw new RuntimeException(MessageConstant.TELEPHONE_VALIDATECODE_NOTNULL);
        }else if (!orderDTO.getValidateCode().equals(code)){
            throw new RuntimeException(MessageConstant.VALIDATECODE_ERROR);
        }

        //  判断会员
        Member member = memberMapper.findByTelephone(orderDTO.getTelephone());
        if (member == null) {
            member = new Member();
            member.setIdCard(orderDTO.getIdCard());
            member.setName(orderDTO.getName());
            member.setSex(orderDTO.getSex());
            member.setPhoneNumber(orderDTO.getTelephone());
            member.setRegTime(new Date());
            member.setPassword(DEFAULT_PASSWORD);
            memberMapper.add(member);
        }

        // 判断预约日期是否可以预约
        OrderSetting orderSetting = orderSettingMapper.findByDate(orderDTO.getOrderDate());
        Order order = orderMapper.findByMemberIdAndOrderDate(member.getId(), orderDTO.getOrderDate());
        if (orderSetting == null){
            throw new RuntimeException(MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }else if (orderSetting.getReservations() >= orderSetting.getNumber()) {
            throw new RuntimeException(MessageConstant.ORDER_FULL);
        }else if (order != null){
            if (orderDTO.getSetmealId().equals(order.getSetmealId())){
                throw new RuntimeException(MessageConstant.HAS_ORDERED);
            }
        }

        Order orderNew = new Order();
        orderNew.setMemberId(member.getId());
        orderNew.setOrderDate(Date.from(orderDTO.getOrderDate().atStartOfDay(ZoneId.systemDefault()).toInstant()));
        orderNew.setOrderType(Order.ORDERTYPE_WEIXIN);
        orderNew.setOrderStatus(Order.ORDERSTATUS_NO);
        orderNew.setSetmealId(orderDTO.getSetmealId());
        orderMapper.add(orderNew);

        orderSettingMapper.reservationsAdd(orderNew.getOrderDate());

    }
}
